package com.hoan.service;

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;

import androidx.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;


public class BookService extends Service {
    private static final String TAG = "BookService";
    private List<Book> bookList;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.i(TAG, "@onCreate");
        bookList = new ArrayList<>();
        initData();
    }

    private void initData() {
        bookList.add(new Book("公主小妹"));
        bookList.add(new Book("仙剑奇侠传"));
        bookList.add(new Book("三生三世十里桃花"));
    }

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.i(TAG, "@onBind");
        return new MyBinder();
    }

    private class MyBinder extends IBookManager.Stub {
        @Override
        public int getInt() throws RemoteException {
            return bookList == null ? 0 : bookList.size();
        }

        @Override
        public String getString() throws RemoteException {
            return bookList == null ? "" : bookList.get(0).getName();
        }

        @Override
        public List<Book> getBookList() throws RemoteException {
            return bookList;
        }

        @Override
        public void addBook(Book book) throws RemoteException {
            if (book != null) {
                bookList.add(book);
            } else {
                Log.i(TAG, "接收到了一个空对象 Inout");
            }
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.i(TAG, "onDestroy");
    }
}



